<template>
	<view class="password-pages page">
		<view class="form-block">
			<view class="form-item">
				<input v-model="form.phone" type="number" maxlength="11" placeholder="请输入手机号码"
					placeholder-class="inputPlaceholder" class="form-ctrl" disabled="true" />
			</view>
			<view class="form-item">
				<input type="number" maxlength="5" v-model="form.mobileCode" placeholder="请输入短信验证码" class="form-ctrl" />
				<view @click="sendAuthCode" class="auth-code-btn">{{btnText}}</view>
			</view>
			<view class="form-item">
				<input v-model="form.paymentPassword" class="form-ctrl" maxlength="6" password="true"
               placeholder="请设置6位支付密码" type="number"/>
			</view>
		</view>
		<view @click="handleSubmit" class="btn">确定</view>
	</view>
</template>

<script>
import {getUserInfo} from '@/api/home.js';
import {validate} from '@/util/validate.js'
import {resetPasswordApi, sendMessageCodeApi} from '@/api/user.js'

export default {
		data() {
			return {
				form: {
					phone: '',
					mobileCode: '',
					paymentPassword: ''
				},
				rules: {
					mobileCode: {
						name: '验证码',
						required: true,
						validator: function(value, form, callback) {
							if (value.length != 5) {
								callback('请输入正确的验证码');
							}
							callback();
						}
					},
					paymentPassword: {
						name: '密码',
						required: true,
						validator: function(value, form, callback) {
              if (value.length !== 6) {
                callback('请输入6位纯数字密码');
              } else {
                const regex = /^\d{6}$/; // 6位纯数字正则表达式
                if (!regex.test(value)) {
                  // 清空输入框
                  form.paymentPassword = '';
                  callback('请输入6位纯数字密码');
                } else {
                  callback();
                }
              }
            }
					},
				},
				btnText: '获取验证码',
				seconds: 0,
			}
		},
		methods: {
			getUser() {
				uni.showLoading({
					title: '加载中！',
					mask: true,
				})
				getUserInfo().then((res) => {
					this.form.phone = res.phone
				})
			},
			sendAuthCode() {
				if (this.seconds == 0) {
					uni.showLoading({
						title: '发送中！',
						mask: true,
					})
					sendMessageCodeApi().then((res) => {
						uni.hideLoading()
						this.showToast('发送成功！');
						this.setCountDown(60)
					}).catch((res) => {
						uni.hideLoading()
						this.showToast('验证码发送失败！');
					})
				}
			},
			setCountDown(seconds) {
				let _this = this;
				this.countDown = setInterval(function() {
					seconds = seconds > 0 ? seconds - 1 : seconds;
					let sendCodeText = seconds > 0 ? `重新获取(${seconds}s)` : '获取验证码';
					_this.btnText = sendCodeText;
					_this.seconds = seconds;
					if (seconds == 0) {
						clearInterval(_this.countDown);
					}
				}, 1000)
			},
			handleSubmit() {
				if (validate(this.form, this.rules)) {
					uni.showLoading({
						title: '提交中',
						mask: true,
					})
					resetPasswordApi(this.form).then((res) => {
						this.showToast('设置成功', () => {
							uni.navigateBack()
						}, 1000)
					})
				}
			}
		},
		onLoad() {
			this.getUser()
		}
	}
</script>

<style lang="scss" scoped>
	.password-pages {
		padding: 30rpx;

		.form-item {
			margin: 16rpx 0;
			width: 100%;
			background: #fff;
			height: 100rpx;
			border-radius: 60rpx;
			font-size: 30rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			padding: 0 30rpx;

			.form-ctrl {
				width: calc(100% - 250rpx);
				height: 100%;
				line-height: 100rpx;
				color: #313131;
			}

			.auth-code-btn {
				width: 250rpx;
				display: flex;
				align-items: center;
				justify-content: flex-end;
				color: #FF7626;
				font-size: 28rpx;
			}
		}

		.btn {
			width: 350rpx;
			height: 80rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			background: linear-gradient(to right, #FF2F34, #FF6B18);
			color: #fff;
			font-size: 28rpx;
			border-radius: 40rpx;
			margin: 0 auto;
			margin-top: 30rpx;
		}
	}
</style>
